package com.power.powercash.report;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 *
 * @author f6036477
 */
public class ColumnInfo {

	private List<Field> fields;

	private int[] order;

	private Object[] values;

	private Field field;

	private List<Object> objects;


	public ColumnInfo() {
		fields = new ArrayList<Field>();
		field = null;
		order = null;
		values = null;
		objects = new ArrayList<Object>();
	}


	private List<Field> fields() {
		return fields;
	}


	public List<Object> objects() {
		return objects;
	}


	public void setObjects(List<Object> objs) {
		objects = objs;
	}


	public void setField(Field f) {
		field = f;
	}


	public Field getField() {
		return field;
	}


	private void toFields() {
		if(objects == null || objects.isEmpty())
			return;

		for(Object o : objects) {
			Field f = new Field(o);
			f.setInfos(field.infos());
			fields.add(f);
		}
	}


	private String[] fieldsToString() {
		if(fields == null || fields.isEmpty()) return null;
		String[] ss = new String[fields.size()];
		for(int i = 0; i < fields.size(); i++) {
			ss[i] = (fields.get(i).getReturn() != null 
					? fields.get(i).getReturn().toString() : "<null>");
		}
		return ss;
	}


	private Object[] fieldsToArray() {
		if(fields == null || fields.isEmpty()) return null;
		Object[] os = new Object[fields.size()];
		for(int i = 0; i < fields.size(); i++) {
			os[i] = fields.get(i).getReturn();
		}
		return os;
	}
	
	
	private void extract() {
		this.toFields();
		Extractor e = new Extractor();
		for(Field f : fields) {
			e.setField(f);
			e.extract();
		}
	}


	public void removeExtracted(Object o) {
		this.extract();
		for(int i = 0; i < fields.size(); i++) {
			Field f = fields.get(i);
			if(f.getReturn().equals(o))
				objects.remove(f.getObject());
		}//for
	}


	private void fillOrder() {
		order = new int[values.length];
		for(int i = 0; i < order.length; i++) {
			order[i] = i;
		}//for
	}


	public Object[] renewValues() {
		this.extract();
		values = this.fieldsToArray();
		this.fillOrder();
		return values;
	}


	public Object[] getValues() {
		if(values == null) {
			return this.renewValues();
		}
		return values;
	}


	public Object[] reorder(int[] neworder) {
		if(values == null) getValues();
		if(values == null || neworder == null) return null;
		if(neworder.length < values.length) return null;

		order = neworder;
		Object[] os = new Object[values.length];

		for(int i = 0; i < values.length; i++) {
			os[i] = values[order[i]];
		}//for
		values = os;
		return values;
	}


	public int[] getOrder() {
		if(order == null) this.fillOrder();
		return order;
	}


	public Object[] sort() {
		if(fields == null
				|| fields.isEmpty()
				|| fields.get(0).getReturn() == null)
			return null;

		this.getValues();
		
		Object[] os = null;
		Object[] tocomp = new Object[values.length];
		if(values[0] instanceof Comparable) {
			os = this.fieldsToArray();
			tocomp = values;
		} else {
			os = this.fieldsToString();
			for(int i = 0; i < values.length; i++) {
				tocomp[i] = values[i].toString();
			}//for
		}

		Arrays.sort(os);

		for(int i = 0; i < os.length; i++) {
			for(int j = 0; j < tocomp.length; j++) {
				if(os[i].equals(tocomp[j])) {
					order[j] = i;
					break;
				}
			}//for
		}//for

		for(int i = 0; i < os.length; i++) {
			for(int j = 0; j < values.length; j++) {
				if(os[i].toString().equals(values[j].toString())) {
					Object t = values[i];
					values[i] = values[j];
					values[j] = t;
				}
			}//for
		}//for

		return values;
	}

}
